﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YRevitJig
{
	public class XRectJig : XUiJigBase
	{
		public XRectJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.JigStage = XRectJig.EStage.eNoOper;
		}

		public XYZ PickStartPoint(string strPrompt = "请确定矩形第一个角点：")
		{
			try
			{
				this.JigStage = XRectJig.EStage.eStartPt;
				this.StartPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.StartPoint = null;
			}
			this.JigStage = XRectJig.EStage.eNoOper;
			return this.StartPoint;
		}

		public XYZ PickRectAngle(XYZ ptStart, string strPrompt = "请确定矩形方向：")
		{
			try
			{
				this.JigStage = XRectJig.EStage.eAnglePt;
				this.StartPoint = ptStart;
				XYZ xyz = base.UiDoc.Selection.PickPoint(strPrompt);
				this.RectAngle = (xyz - this.StartPoint).Normalize();
			}
			catch
			{
			}
			this.JigStage = XRectJig.EStage.eNoOper;
			return this.RectAngle;
		}

		public XYZ PickEndPoint(XYZ ptStart, XYZ vtAngle, string strPrompt = "请确定矩形另一对角点：")
		{
			try
			{
				this.JigStage = XRectJig.EStage.eEndPt;
				this.StartPoint = ptStart;
				this.RectAngle = vtAngle;
				this.EndPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.EndPoint = null;
			}
			this.JigStage = XRectJig.EStage.eNoOper;
			return this.EndPoint;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			bool flag = this.JigStage == XRectJig.EStage.eAnglePt;
			if (flag)
			{
                System.Drawing.Point ptStart = base.Revit2Client(this.StartPoint);
				Graphics graphics = paintEventArgs.Graphics;
				XLine2D xline2D = new XLine2D(ptStart, this.m_ptCur);
				XGraphics g = new XGraphics(graphics, base.JigPen);
				xline2D.Draw(g);
			}
			else
			{
				bool flag2 = this.JigStage == XRectJig.EStage.eEndPt;
				if (flag2)
				{
					try
					{
						XYZ xyz = base.Client2Revit(this.m_ptCur);
						Line line = Line.CreateUnbound(this.StartPoint, this.RectAngle);
						XYZ xyzpoint = line.Project(xyz).XYZPoint;
						XYZ point = xyz + (this.StartPoint - xyzpoint);
                        System.Drawing.Point point2 = base.Revit2Client(this.StartPoint);
                        System.Drawing.Point point3 = base.Revit2Client(xyzpoint);
                        System.Drawing.Point point4 = base.Revit2Client(point);
						Graphics graphics2 = paintEventArgs.Graphics;
						XGraphics g2 = new XGraphics(graphics2, base.JigPen);
						XLine2D xline2D2 = new XLine2D(point2, point3);
						xline2D2.Draw(g2);
						XLine2D xline2D3 = new XLine2D(point3, this.m_ptCur);
						xline2D3.Draw(g2);
						XLine2D xline2D4 = new XLine2D(this.m_ptCur, point4);
						xline2D4.Draw(g2);
						XLine2D xline2D5 = new XLine2D(point4, point2);
						xline2D5.Draw(g2);
					}
					catch
					{
					}
				}
			}
		}

		public override List<Curve> CreateCurves()
		{
			List<Curve> list = new List<Curve>();
			try
			{
				Line line = Line.CreateUnbound(this.StartPoint, this.RectAngle);
				XYZ xyzpoint = line.Project(this.EndPoint).XYZPoint;
				XYZ xyz = this.EndPoint + (this.StartPoint - xyzpoint);
				Line item = Line.CreateBound(this.StartPoint, xyzpoint);
				list.Add(item);
				Line item2 = Line.CreateBound(xyzpoint, this.EndPoint);
				list.Add(item2);
				Line item3 = Line.CreateBound(this.EndPoint, xyz);
				list.Add(item3);
				Line item4 = Line.CreateBound(xyz, this.StartPoint);
				list.Add(item4);
			}
			catch
			{
			}
			return list;
		}

		public XYZ StartPoint { get; set; }

		public XYZ RectAngle { get; set; }

		public XYZ EndPoint { get; set; }

		public XRectJig.EStage JigStage { get; set; }

		public enum EStage
		{
			eNoOper,
			eStartPt,
			eAnglePt,
			eEndPt
		}
	}
}
